package com.chezhujie.www.service;

import com.chezhujie.www.core.exception.ClassCheckException;
import com.chezhujie.www.core.page.Pagination;
import com.chezhujie.www.domain.${simpleClassName};
import com.chezhujie.www.persistence.${simpleClassName}Mapper;
import com.chezhujie.www.utils.UUIDUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * ${describe}
 * Created by 汤伟杰 on 2017/5/23.
 */
@Service
public class ${simpleClassName}Service extends BaseService<${simpleClassName}>{
    @Autowired
    private ${simpleClassName}Mapper ${simpleClassname}Mapper;

    @Autowired
    public void setBaseMapper(${simpleClassName}Mapper ${simpleClassname}Mapper) {
        this.baseMapper = ${simpleClassname}Mapper;
    }

   <#-- public Pagination list(int pageNo, int pageSize, Map<String, Object> params, ModelMap model) throws Exception {

        Pagination pagination = new Pagination();
        pagination.setPageNo(pageNo);
        pagination.setPageSize(pageSize);
        params.putAll(pagination.toMap());
        int count = ${simpleClassname}Mapper.getTotalRows(params);
        List<Map<String, Object>> list = ${simpleClassname}Mapper.getList(params);
        pagination.setTotalCount(count);
        pagination.setList(list);
        return pagination;
    }


    public void insert(${simpleClassName} ${simpleClassname}){
        if(StringUtils.isNotBlank(${simpleClassname}.get${keyPropertiesUp}())){
        ${simpleClassname}.set${keyPropertiesUp}(UUIDUtils.getUUID());
        }
        ${simpleClassname}Mapper.insert(${simpleClassname});
    }

    public void update(${simpleClassName} ${simpleClassname}){
        ${simpleClassname}Mapper.update(${simpleClassname});
    }

    public void deleteById(String ${keyProperties}){
        ${simpleClassname}Mapper.deleteById(${keyProperties});
    }

    public void deleteByIds(List<String> list){
        ${simpleClassname}Mapper.deleteByIds(list);
    }

    public ${simpleClassName} queryById(String id){
        return ${simpleClassname}Mapper.queryById(id);
    }

    public List<${simpleClassName}> queryByIds(List<String> list){
        List<${simpleClassName}> list${simpleClassName} = ${simpleClassname}Mapper.queryByIds(list);
        if(list${simpleClassName}==null){
            list${simpleClassName} = new ArrayList<${simpleClassName}>();
        }
        return list${simpleClassName};
    }

    public void saves(List<${simpleClassName}> list){
        ${simpleClassname}Mapper.saves(list);
    }-->
    <#list columnList as var>
        <#if 'select'==var.inputType&&('int'==var.inputCode||'str'==var.inputCode)>
    public List<Map<String,Object>> getList${var.propertiesUp}(Map<String, Object> param) {
        return ${simpleClassname}Mapper.list${var.propertiesUp}(param);
    }
        </#if>
    </#list>

    public void checkEntity(${simpleClassName} ${simpleClassname}) throws ClassCheckException {
        <#list listString as str>
        if(${simpleClassname}.get${str.propertiesUp}()!=null&&${simpleClassname}.get${str.propertiesUp}().length()>${str.length}){
            throw new ClassCheckException("${describe}${str.describe}不能超过${str.length}个字符");
        }
        </#list>
        <#list listFile as file>
        if(${simpleClassname}.get${file.propertiesUp}()!=null&&${simpleClassname}.get${file.propertiesUp}().length()>${file.length}){
            throw new ClassCheckException("${describe}${file.describe}不能超过${file.length}个字符");
        }
        </#list>
    }
    public void checkListEntity(List<${simpleClassName}> list${simpleClassName}) throws ClassCheckException {

        for (${simpleClassName} ${simpleClassname} : list${simpleClassName}) {
            checkEntity(${simpleClassname});
        }

    }
}
